﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace UIT.Utils
{
    internal class TokenizerHelper
    {
        private char _quoteChar;
        private char _argSeparator;
        private string _str;
        private int _strLen;
        private int _charIndex;
        internal int _currentTokenIndex;
        internal int _currentTokenLength;
        private bool _foundSeparator;
        internal bool FoundSeparator
        {
            get
            {
                return this._foundSeparator;
            }
        }
        internal TokenizerHelper(string str, IFormatProvider formatProvider)
        {
            char numericListSeparator = TokenizerHelper.GetNumericListSeparator(formatProvider);
            this.Initialize(str, '\'', numericListSeparator);
        }
        internal TokenizerHelper(string str, char quoteChar, char separator)
        {
            this.Initialize(str, quoteChar, separator);
        }
        internal string GetCurrentToken()
        {
            if (this._currentTokenIndex < 0)
            {
                return null;
            }
            return this._str.Substring(this._currentTokenIndex, this._currentTokenLength);
        }
        internal void LastTokenRequired()
        {
            if (this._charIndex != this._strLen)
            {
                throw new InvalidOperationException(string.Format("TokenizerHelperExtraDataEncountered {1} at {0}", new object[]
				{
					this._charIndex, 
					this._str
				}));
            }
        }
        internal bool NextToken()
        {
            return this.NextToken(false);
        }
        internal string NextTokenRequired()
        {
            if (!this.NextToken(false))
            {
                throw new InvalidOperationException(string.Format("TokenizerHelperPrematureStringTermination after {0}", this._str));
            }
            return this.GetCurrentToken();
        }
        internal string NextTokenRequired(bool allowQuotedToken)
        {
            if (!this.NextToken(allowQuotedToken))
            {
                throw new InvalidOperationException(string.Format("TokenizerHelperPrematureStringTermination afer {0}", this._str));
            }
            return this.GetCurrentToken();
        }
        internal bool NextToken(bool allowQuotedToken)
        {
            return this.NextToken(allowQuotedToken, this._argSeparator);
        }
        internal bool NextToken(bool allowQuotedToken, char separator)
        {
            this._currentTokenIndex = -1;
            this._foundSeparator = false;
            if (this._charIndex >= this._strLen)
            {
                return false;
            }
            char c = this._str[this._charIndex];
            int num = 0;
            if (allowQuotedToken && c == this._quoteChar)
            {
                num++;
                this._charIndex++;
            }
            int charIndex = this._charIndex;
            int num2 = 0;
            while (this._charIndex < this._strLen)
            {
                c = this._str[this._charIndex];
                if (num > 0)
                {
                    if (c == this._quoteChar)
                    {
                        num--;
                        if (num == 0)
                        {
                            this._charIndex++;
                            break;
                        }
                    }
                }
                else
                {
                    if (char.IsWhiteSpace(c) || c == separator)
                    {
                        if (c == separator)
                        {
                            this._foundSeparator = true;
                            break;
                        }
                        break;
                    }
                }
                this._charIndex++;
                num2++;
            }
            if (num > 0)
            {
                throw new InvalidOperationException(string.Format("TokenizerHelperMissingEndQuote for {0}", this._str));
            }
            this.ScanToNextToken(separator);
            this._currentTokenIndex = charIndex;
            this._currentTokenLength = num2;
            if (this._currentTokenLength < 1)
            {
                throw new InvalidOperationException(string.Format("TokenizerHelperEmptyToken after {1} at {0}", this._charIndex, this._str));
            }
            return true;
        }
        internal static char GetNumericListSeparator(IFormatProvider provider)
        {
            char c = ',';
            NumberFormatInfo instance = NumberFormatInfo.GetInstance(provider);
            if (instance.NumberDecimalSeparator.Length > 0 && c == instance.NumberDecimalSeparator[0])
            {
                c = ';';
            }
            return c;
        }
        private void Initialize(string str, char quoteChar, char separator)
        {
            this._str = str;
            this._strLen = ((str == null) ? 0 : str.Length);
            this._currentTokenIndex = -1;
            this._quoteChar = quoteChar;
            this._argSeparator = separator;
            while (this._charIndex < this._strLen)
            {
                if (!char.IsWhiteSpace(this._str, this._charIndex))
                {
                    return;
                }
                this._charIndex++;
            }
        }
        private void ScanToNextToken(char separator)
        {
            if (this._charIndex < this._strLen)
            {
                char c = this._str[this._charIndex];
                if (c != separator && !char.IsWhiteSpace(c))
                {
                    throw new InvalidOperationException(string.Format("TokenizerHelperExtraDataEncountered after {1} at {0}", this._charIndex, this._str));
                }
                int num = 0;
                while (this._charIndex < this._strLen)
                {
                    c = this._str[this._charIndex];
                    if (c == separator)
                    {
                        this._foundSeparator = true;
                        num++;
                        this._charIndex++;
                        if (num > 1)
                        {
                            throw new InvalidOperationException(string.Format("TokenizerHelperEmptyToken after {1} at {0}", this._charIndex, this._str));
                        }
                    }
                    else
                    {
                        if (!char.IsWhiteSpace(c))
                        {
                            break;
                        }
                        this._charIndex++;
                    }
                }
                if (num > 0 && this._charIndex >= this._strLen)
                {
                    throw new InvalidOperationException(string.Format("TokenizerHelperEmptyToken", this._charIndex, this._str));
                }
            }
        }
    }
}
